const app = getApp();
const audioCtx = wx.createAudioContext('myAudio')
const innerAudioContext = wx.getBackgroundAudioManager();
const innerAudioContextTwo = wx.createInnerAudioContext();
Component({
  properties: {
    // 这里定义了innerText属性，属性值可以在组件使用时指定
    receiveData: {
      type: JSON,
      value: '',
   
    }
  },
  data: {
    // 这里是一些组件内部数据
    someData: {},
    playState:false,
    setting:{},
    curTime:{value:0,showStr:"00:00"},
    allTime:{value:0,showStr:"00:00"},
  },
  // 组件本身自带ready方法，自带的方法写在methods之外
   ready:function(){
       let that=this;
       that.setData({
          sysWidth: app.globalData.sysWidth,
          setting:app.globalData.setting,
       });
       let allTime=that.data.allTime;
       innerAudioContextTwo.src = that.data.receiveData.jsonData.source;
       innerAudioContextTwo.onCanplay(() => {
               innerAudioContextTwo.duration //类似初始化-必须触发-不触发此函数延时也获取不到
               setTimeout(function () {
                 allTime.value=parseInt(innerAudioContextTwo.duration);
                 allTime.showStr=that.setTimeFun(innerAudioContextTwo.duration)
                 //在这里就可以获取到大家梦寐以求的时长了
                 console.log(innerAudioContextTwo.duration); //延时获取长度 单位：秒
                 that.setData({allTime:allTime})
               }, 500)  //这里设置延时500毫秒获取
             });
       console.log("======audio=====",that.data.receiveData,that.data.allTime,allTime);
   },
  methods: {
    playAudioFun:function(state,data){
        let that=this;
        console.log("===playAudioFun===",state);
        let playState=(state=='play'?true:!that.data.playState);
        if(playState){
            wx.playBackgroundAudio({
              dataUrl: that.data.receiveData.jsonData.source,
              title: that.data.receiveData.jsonData.name,
              coverImgUrl: that.data.receiveData.jsonData.posterImg
            })
            innerAudioContext.onTimeUpdate(() => {
                  innerAudioContext.currentTime
                  let curTime=data?data:that.data.curTime;
                  let allTime=that.data.allTime;
                  let currentTime=parseInt(innerAudioContext.currentTime);
                  if(!allTime.value){
                      innerAudioContext.duration
                      let duration=parseInt(innerAudioContext.duration);
                      allTime.value=duration;
                      allTime.showStr=that.setTimeFun(duration)
                      that.setData({allTime:allTime})
                  }
                  curTime.value=currentTime;
                  curTime.showStr=that.setTimeFun(currentTime)
                  that.setData({curTime:curTime});
            })
            innerAudioContext.onEnded(() => {
                  console.log("结束了",);
                  that.setData({playState:false})
            })
        }else{
            wx.pauseBackgroundAudio()
        }
        that.setData({playState:playState})
    },
    sliderChangeingFun:function(data){
        console.log("===sliderChangeingFun====",data,audioCtx)
        let that=this;
        if(that.data.playState){
            wx.pauseBackgroundAudio()
        }
    },
    sliderChangeFun:function(data){
        console.log("===sliderChangeFun====",data,audioCtx);
        let that=this;
        let curTime=that.data.curTime;
        let time=parseInt(data.detail.value);
        wx.seekBackgroundAudio({
          position: time
        })
        curTime.value=time;
        curTime.showStr=that.setTimeFun(time)
        that.setData({curTime:curTime});
        that.playAudioFun('play',curTime);
        console.log("====curTime====",curTime,that.data.curTime)
    },
    setTimeFun:function(data){
        let that=this;
        let dataNum=parseInt(data)
        if(dataNum>=60){
           let minutes= parseInt(dataNum/60);
           let seconds=dataNum%60;
           let resultData=(minutes>=10?minutes:'0'+minutes)+":"+(seconds>=10?seconds:'0'+seconds)
           return resultData
        }else{
            return "00:"+(data.toFixed(0)>=10?data.toFixed(0):'0'+data.toFixed(0));
        }
    },
    audioTimeupdate:function(data){
        let that=this;
        let curTime=that.data.curTime;
        let time=parseInt(data.detail.currentTime);
        curTime.value=time;
        curTime.showStr=that.setTimeFun(time)
        // that.setData({curTime:curTime});
    },
    stopAudioFun:function(){
        wx.stopBackgroundAudio()
    },
    // 这里是一个自定义方法
    audioPlay: function (e) {
      console.log(e.currentTarget.dataset.index)
      const innerAudioContext = wx.createInnerAudioContext()
      innerAudioContext.autoplay = true
      innerAudioContext.src ="http://p08e0oy3g.bkt.clouddn.com/%E8%A5%BF%E5%96%B5iy%20-%20%E4%BD%A0%E8%8B%A5%E6%88%90%E9%A3%8E.mp3"
      console.log(innerAudioContext.currentTime)
      
      innerAudioContext.onPlay(() => {
        console.log('开始播放')
      })
      innerAudioContext.onError((res) => {
        console.log(res.errMsg)
        console.log(res.errCode)
      })
    },

  },

})